<?php
//
// methods for scanning event files

if ($do === 'importFiles') {
   // scan dir for files; you can find the getDirFiles function in /includes/libccramp.inc 
   $evtFiles = getDirFiles($MUSIC_DIR.'EVENTS/INCOMING/');
   
   if (!is_dir($MUSIC_DIR.'EVENTS/')) mkdir($MUSIC_DIR.'EVENTS/', 0744);
   if (!is_dir($MUSIC_DIR.'EVENTS/UNSORTED/')) mkdir($MUSIC_DIR.'EVENTS/UNSORTED/', 0744);
   
   $evtTypeQuery = mysql_query("SELECT `filename` FROM `event` ");
   while ($evtType = mysql_fetch_array($evtTypeQuery)) {
      if (!is_dir($MUSIC_DIR.'EVENTS/'.$evtType[filename])) {
         mkdir($MUSIC_DIR.'EVENTS/'.$evtType[filename], 0744);
      }
   }
   
   if (!$evtFiles) {
     echo ('Sorry, nothing to do.  Please check to make sure your files are in '.$MUSIC_DIR.'EVENTS/INCOMING/ on the host machine.<br />');
   } else {
      
      for ($i = 0; $i < count($evtFiles); $i++) {
         $file = pathinfo($evtFiles[$i]);
         echo ('<h1>'.$file[basename].'</h1>');
         mysql_query(" INSERT INTO `event_files` 
            SET 
            `filepathname`='".$file[basename]."',
            `eventtype`='".$_REQUEST[eventtype]."'
            ") or die(mysql_error());
         $newPath = $MUSIC_DIR.'EVENTS/UNSORTED/';
         rename ($evtFiles[$i], $newPath.$file[basename]);
      }
   }
} else {
?>
<form action="?mode=scanEvtFiles" method="post">
files will be imported from "<?php echo($MUSIC_DIR.'EVENTS/INCOMING/') ?>"<br />
<select name="eventtype"><option value="">UNSORTED</option>
<?php 
$evtQuery = mysql_query("SELECT * FROM `event` ORDER BY `name` ASC ");
while ($evt = mysql_fetch_array($evtQuery)) {
   printf('<option value="%s">%s</option>', $evt[id], $evt[name]);
}
?>
</select>
<input type="hidden" name="do" value="importFiles" />
<input type="submit" value="Scan" />
</form>

<?php 
}

?>